<template>
	<view>
		<view>
			{{payment.ghf.sum}}
		</view>
		<button type="default" @click="open_wexin()">打开小程序</button>
		<button type="default" @click="open_abc()">打开农行app</button>
		<button type="default" @click="open_Camera()">打开摄像头</button>
		<button type="default" @click="open_album()">打开相册</button>
		<button type="default" @click="abc()">abc</button>
		<button type="default" @click="get_Bearer()">get_Bearer</button>
    <button type="default" @click="abcPay()">abcPay</button>
    <input type="text" v-model="TokenID" class="border b-radius-10">
    <view>
			ver:{{ver}}
		</view>

		<view class="bbtc-box" v-show="isTrue">
			<view class="bg" @touchmove.stop.prevent="moveHanle"></view>
			<view class="bb-box">

				<view class="gui-color-orange gui-h3">发现新版本-V{{gxInfo.version}}</view>

				<view class="bb-nr">
					<text><text class="text-orange" style="padding: 0 10rpx;">●</text> {{gxInfo.content }}</text>
				</view>
				<view class="gx" @click="gx()">
					<!-- <button>{{buttonText}}</button>
					<view>{{progress}}</view> -->
					<gui-progress-button :buttonText="buttonText" borderRadius="50rpx"
						:val="progress"></gui-progress-button>
				</view>
				<view class="bgx text-grey-74" @click="isTrue = false">暂不更新</view>
			</view>
		</view>

	</view>
</template>

<script>
	var ABCPay

	export default {
		data() {
			return {
				payment: {
					ghf: {
						sum: 0
					}
				},
				user: uni.getStorageSync("userInfo"),
				ver: 0,
				isTrue: false,
				gxInfo: {},
				buttonText: "立即更新",
				isreplace: false,
				progress: 0,
        TokenID:""
			}
		},
		onLoad() {
			console.log(this.user);
			// #ifdef APP
			ABCPay = uni.requireNativePlugin('ABC-Pay');
			// #endif
			uni.setStorageSync("Bearer",
				"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiI5NDczMGI4Yi1lYWZhLTRlZGItYWExNy1jYzVjNDlmODI4MzQiLCJqdGkiOiJmNjdlMDc2MjJjZjczMmYxODc3ZTZhNDY1Y2I0MWQ3Mzc3OGUzYzE0MDg0MTAxOTUxYjA1ODI0NDk3Nzk4Mzg5ZjFiZmIwYjRhM2QzMzAyMCIsImlhdCI6MTcyMjYyNzI1Ni4wNzc2ODksIm5iZiI6MTcyMjYyNzI1Ni4wNzc2OSwiZXhwIjoxNzM4NTI0ODU2LjA3MjU3Miwic3ViIjoiMzAiLCJzY29wZXMiOltdfQ.g8BBoP0rK2N7sIsth7BZkkJ_yUSlLmtqCemPxResRTbR-_63YP5oNWot2lyiXmRHlKRFIEHJSPIlBOLd_lLvBJ--IMAQ65IJFrLQYsBRNjTiLYyr3NVSXHJiNwnyIx6YdxNldiJWY0y2PyduaW3aDCfsy6ULwpmIav-KYlAQOROe7rOfmim1_oJUt2JRDr3lXSCzxOd9FtlRRI6ywfhHCA6_vKIqb0QhZQtIO5vwNOYcNG8WaxvrdVMNaLCq36kelGQcFc4NLNOa5wXj1kOCUeGg1EV6gSctiUFQ5rXHcNQ_Cx5cHxMKGKqo8YrsQCvcty2-bBSybfS9cIuuxSVEXQWZtS4R-kzXAp9_wTrh0dVcoKRmn2QcBsu1BN-IngaXRPpxmPGCkLrZXjnA9MfR7oKYZYq0ND0Ap7BxKVDfYEQhF66z_QMOlSvsxpVql1xM3YFq4Vzp7U529aMPzp5SgwVccoAsqvCeEqGkVAqhCnWfgzbm5DDf8Qub4LbUFP1S6eJUzgW-a2tYIH9GNfugnprbdSMcLvzwZcXgkE719R-MDsCGttXCSNnFXvEgrAQUwS1WJ5Etx2xdCHrNMn1c09Sfjsp4-MCbLWFkBPZbwjluWJpgptDnY3QsXowvfR_X33icZEJQ7IQHdLiq06Y26r1nluLlYSysakTHqPEF0H8"
				)
			//let ret = ABCPay.isBankABCAvaiable();
			//console.log("农行APP是否安装："+ret);
		},
		created() {
			// let info= uni.getSystemInfoSync();
			// this.ver=info.appVersion;
			// let self=this;
			// this.checkWgt(function(res){
			// 	console.log(res);
			// 	self.gxInfo = res;
			// 	console.log("gxInfo",self.gxInfo);
			// 	self.isTrue = true;
			// })
			// uni.$once('Bearer',function(data){
			// 		console.log('监听到事件来自 Bearer ，携带参数 msg 为：' + data.Bearer);
			// 		uni.showToast({
			// 			title:data.Bearer
			// 		})
			// 	})

		},
		methods: {
      abcPay(){
        ABCPay.startBankABC({
          appID: "com.bjttsx.liugan", //第三方APP的应用标识。iOS无此项内容；Android默认为第三方apk的包名，(可选)
          Method:"pay",//业务类型。目前支持的取值仅为pay，表示是支付交易。默认值pay，(可选)
          CallbackID: "", //支付完成后回调用的id。iOS为第三方app的schema，(iOS必需，Android可选)
          TokenID: this.TokenID //支付订单号(必需)
        }, function(res) {
          console.log(res);
          if (res.code == 0) {
            //支付结果
            console.log(res.data);
          } else {
            uni.showModal({
              content: res.data
            });
          }
        });
      },
			get_Bearer() {
				uni.webView.postMessage({
					data: {
						type: "get_Bearer",
						data: {

						}
					},
				})
			},
			open_wexin() {
				uni.webView.postMessage({
					data: {
						type: "open_wexin",
						data: {
							app_id: "gh_edf4ceb10043",
							path: "pages/jf/wxpay?code=0.03-J2824F780868780872780924T05195744R55-0"
						}
					},
				})
			},
			open_abc() {
				uni.webView.postMessage({
					data: {
						type: "open_abc",
						data: {
							
						}
					},
				})

			},
			open_Camera() {
				uni.webView.postMessage({
					data: {
						type: "Camera",
						data: {
							
						}
					},
				})
			},
			open_album() {
				uni.webView.postMessage({
					data: {
						type: "album",
						data: {
							
						}
					},
				})
			},
			abc() {
				let mch_id = "JF-EPAY2024051619799"; //雀丰
				// let mch_id="JF-EPAY2019062100960";  //boya
				let appid = "gh_a6986a2f785e";
				let path = "page/containers/payment/query-input/query-input?code=" + mch_id
				uni.webView.postMessage({
					data: {
						type: "open_wexin",
						data: {
							app_id: appid,
							path: path
						}
					},
				})
			},

			pay_ghf() {
				// #ifdef APP-PLUS
				this.$boya.NGet("/app/abc/ghf_order", {
					"phone": this.user.phone_number
				}, function(res) {
					console.log("ghf_order", res);
					ABCPay.startBankABC({
						appID: "com.bjttsx.liugan", //第三方APP的应用标识。iOS无此项内容；Android默认为第三方apk的包名，(可选)
						//Method:"pay",//业务类型。目前支持的取值仅为pay，表示是支付交易。默认值pay，(可选)
						CallbackID: "", //支付完成后回调用的id。iOS为第三方app的schema，(iOS必需，Android可选)
						TokenID: res.data.token //支付订单号(必需)
					}, function(res) {
						console.log(res);
						if (res.code == 0) {
							//支付结果
							console.log(res.data);
						} else {
							uni.showModal({
								content: res.data
							});
						}
					});
				});
				// #endif
			},
			get_ghf() {
				let self = this;
				console.log("get_ghf");
				this.$boya.NGet("/app/query_ghf", {
					"phone": this.user.phone_number
				}, function(res) {
					console.log("query_ghf", res);

					self.payment.ghf = res.data;
					console.log(self.payment.ghf);
				});
			},
			checkWgt: function(success) {
				// #ifdef APP-PLUS
				let that = this;
				let appHj = 1;
				if (uni.getSystemInfoSync().platform == 'android') {
					appHj = 1;
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					appHj = 2;
				}
				console.log("platform", appHj);

				plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
					console.log("getProperty", widgetInfo);
					//请求获取最新版本信息

					that.$boya.NGet("/app/getVer", {
						VersionType: appHj
					}, function(res) {
						console.log("/app/getVer", res);
						// });
						// that.RequestData({
						// 	url:that.getVersion,
						// 	data:{VersionType:appHj}
						// },(result)=>{
						let result = res.data;
						console.log(result, "checkWgt");
						//result={"VersionNumber":"2.7.5","VersionType":1,"Describe":"修复bug","versioncode":275,"QrCodeUrl":"https://tc.gxcbcmjt.com/cache/20221204/31b368ab5ecd42a182caad46b151469a.apk","IsHotUpdate":true,"MiniVer":"274","Id":79,"Guid":"32d5f1ce-a34b-4e02-8623-301d89d99bc1","CreatedDate":"2022-12-04 15:32:23","LastModifiedDate":"2022-12-04 15:32:23"} ;
						let minVersion = 0;


						let {
							version_code,
							content,
							version,
							is_wgt
						} = result;
						let down_url = result.url;
						if (result.hasOwnProperty("minVersion")) {
							minVersion = result.minVersion;
						}
						console.log("ver->minver,is_wgt", is_wgt);
						console.log(parseInt(widgetInfo.versionCode), parseInt(minVersion))
						// 如果安装的版本号小于最新发布的版本号，则进行更新
						if (parseInt(widgetInfo.versionCode) < parseInt(version_code)) {
							// 如果是热更新，则执行静默更新
							if (is_wgt == 1 && parseInt(widgetInfo.versionCode) > parseInt(
								minVersion)) {

								uni.showModal({
									title: "更新提示",
									content: "应用优化更新，是否需要更新应用？",
									success: (rex) => {
										if (rex.confirm) {
											// 下载wgt更新包
											uni.downloadFile({
												url: down_url,
												success: (downloadResult) => {
													if (downloadResult
														.statusCode === 200) {
														plus.runtime.install(
															downloadResult
															.tempFilePath, {
																force: true // 强制更新
															},
															function() {
																console
																	.log(
																		'热更新成功'
																		);
																uni.showModal({
																	showCancel: false,
																	title: "提示",
																	content: "应用更新完成，需要重启应用",
																	success: (
																		rex2
																		) => {
																		if (rex2
																			.confirm
																			) {
																			that
																		.nativeQuit();
																			// plus.runtime.restart();
																		}
																	}
																})
															},
															function(e) {
																console
																	.error(
																		'热更新失败，错误原因：' +
																		e);
															});
													} else {
														console.log('下载wgt失败');
													}
												}
											})
										}
									}
								})
							} else {
								console.log('版本过低，请使用整包大版本更新');
								success(result);
							}
						} else {
							console.log('你的版本为最新，不需要更新');
						}
					})
				});
				// #endif
			},
			nativeQuit: function() {
				let _this = this;
				console.log("nativeQuit");
				uni.showToast({
					title: "准备重新启动！"
				});
				uni.getSystemInfo({
					success: function(e) {
						console.log(e.platform)
						Vue.prototype.StatusBar = e.statusBarHeight;
						Vue.prototype.$phoneInfo = e
						// let system = plus.android.import('java.lang.System')

						if (e.platform == 'android') {
							let main = plus.android.runtimeMainActivity();
							//  热更新之后需要杀死进程重新打开,需要引入安卓system的类来实现
							let system = plus.android.importClass('java.lang.System')
							system.exit(0);
						} else {
							plus.ios.import('UIApplication').sharedApplication().performSelector('exit');
						}
					}
				})
			},
			moveHanle() {},
			gx() {
				let that = this;

				if (uni.getSystemInfoSync().platform == 'android') {
					that.buttonText = "请稍等...";
					if (that.isreplace) {
						return;
					}
					that.isreplace = true;
					console.log("下载地址：" + that.gxInfo.url);
					const downloadTask = uni.downloadFile({
						url: that.gxInfo.url,
						success: (res) => {
							if (res.statusCode === 200) {
								that.progress = 100;
								that.buttonText = '下载完成,即将重启应用';
								plus.runtime.install(res.tempFilePath, {
									force: true // 强制更新
								}, function() {
									console.log("下载成功")
									that.$common.nativeQuit();
								}, function(error) {
									console.error('安装失败，错误原因：' + error);
									uni.showToast({
										title: '安装失败',
										icon: "error",
										duration: 1500
									});
								})
								setTimeout(res => {
									that.isreplace = false;
								}, 1000)
							}
						},
						complete: () => {}
					})
					downloadTask.onProgressUpdate((resx) => {
						console.log('下载进度' + resx.progress);
						that.progress = resx.progress;
					});
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					plus.runtime.launchApplication({
						action: that.gxInfo.url
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
			},
		}
	}
</script>

<style>
	/* 版本更新弹窗样式 */
	.bbtc-box,
	.bg {
		width: 750rpx;
		height: 100%;
		overflow: hidden;
		background: rgba(0, 0, 0, 0.3);
		position: fixed;
		flex: 1;
		top: 0;
		bottom: 0;
		left: 0;
		z-index: 9999999;
	}

	.logos {
		width: 150rpx;
		height: 150rpx;
		margin-top: -80rpx;
	}

	.bb-bt {
		padding: 22% 0 0 6%;
		font-size: 34rpx;
		color: #fff;
		font-weight: bold;
		text-align: left;
	}

	.bb-bt2 {
		padding: 2% 0 0 6%;
		font-size: 28rpx;
		color: #fff;
		font-weight: bold;
		text-align: left;
	}

	.bb-nr {
		padding-top: 16%;
	}

	.bb-nr>text {
		font-size: 28rpx;
		padding: 20rpx 30rpx;
		display: block;
		text-align: left;
	}

	.gx {
		width: 400rpx;
		margin: 40rpx auto 26rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 28rpx;
		background: #F68512;
		border-radius: 50rpx;
	}

	.bgx {
		text-align: center;
		margin: 25rpx 0 20rpx;
	}

	.logo {
		text-align: center;
		width: 100%;
		padding: 30rpx 0;
	}

	.logo image {
		width: 350rpx;
		height: 100rpx;
	}

	.bb-box {
		width: 70%;
		text-align: center;
		border-radius: 15rpx;
		padding: 15rpx;
		background: #fff;
		position: absolute;
		top: 20%;
		left: 0;
		right: 0;
		z-index: 9999999;
		margin: auto;
	}

	/* 版本更新弹窗样式 */
</style>